<?php
    class GP_trad
    {
        static $_instance;
        public $_language;
        public $_db;
        
        // singleton
		public function &getInstance($language) 
		{
			if (!self :: $_instance instanceof self) {
				self :: $_instance = new self($language);
            }
			return self :: $_instance;
		}
        
        function __construct($language)
        {
            $this->_language = $language;
            $this->_db = GP_jdb::getInstance(config('translation.dbname'), $language);
        }
        
        function __($str)
        {
            if (null === $this->_language) {
                return $str;
            }
            $idTrad = md5($str . $this->_language);
            $res = $this->_db->fetchArray($this->_db->search('idTrad = ' . $idTrad, true));
            if (null !== $res) {
                return $res['translation'];
            } else {
                $insert = array(
                    'language' => $this->_language, 
                    'ref' => $str, 
                    'translation' => $str, 
                    'idTrad' => $idTrad
                );
                $ins = $this->_db->insert($insert);
                return $str;
            }
        }
    }
?>